/*
 * Copyright (C) 2015 yixun All Rights Reserved.
 * 
 * AreaServiceImpl.java
 */
package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.FileAttachmentService;
import com.yixun.qfbao.apidto.GetLogoReqDto;
import com.yixun.qfbao.apidto.GetLogoResDto;
import com.yixun.qfbao.mapper.CompanyMapper;
import com.yixun.qfbao.mapper.LogoMapper;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Logo;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.LogoDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * [文件名称]<br>
 * FileAttachmentServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 *  文件、图片相关<br>
 * <br>
 * @author JHSong
 * @version 1.00
 */
@Service
public class FileAttachmentServiceImpl implements FileAttachmentService {
	
	@Value("${system.domain}")
	private  String domainName;
	
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private LogoMapper logoMapper;
	
	@Override
	public GetLogoResDto getLogo(GetLogoReqDto getLogoReqDto) {
		GetLogoResDto resDto = new GetLogoResDto();
		String companyUuid = getLogoReqDto.getCompanyUuid();
		String logoType = getLogoReqDto.getLogoType();
		if(StringUtils.isBlank(companyUuid)){
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("公司信息获取失败！");
			return resDto;
		}

		if(StringUtils.isBlank(logoType)){
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("图片类型为空!");
			return resDto;
		}

		CompanyDto dto = new CompanyDto();
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		dto.setCompanyUuid(companyUuid);
		Company company = companyMapper.selectByDto(dto);
		if(company != null && company.getId() != null){
			LogoDto logoDto = new LogoDto();
			logoDto.setLogoMerchantid(company.getId());
			logoDto.setLogoType(logoType);
			logoDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<Logo> logoList = logoMapper.selectListByDto(logoDto);
			if(null != logoList && logoList.size()>0){
				resDto.setResult(ResultEnum.SUCCESS.getNo());
				resDto.setLogoUrl(logoList.get(0).getLogoUrl());
			}else{
				resDto.setResult(ResultEnum.FAIL.getNo());
				resDto.setLogoUrl("未找到对应的logo!");
			}
		}else{
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setLogoUrl("通过域名未找到对应的公司！");
		}
		return resDto;
	}

	public String getDomainName() {
		return domainName;
	}

}
